package com.yjc.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjc.blog.domain.bo.DocDao;
import com.yjc.blog.domain.bo.DocSave;
import com.yjc.blog.domain.po.Doc;
import com.yjc.blog.domain.vo.DocVO;
import com.yjc.blog.domain.vo.PageVO;
import com.yjc.blog.mapper.DocMapper;
import com.yjc.blog.service.IDocService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjc.blog.utils.CopyUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 文档 服务实现类
 * </p>
 *
 * @author yjc
 * @since 2024-11-01
 */
@Service
public class DocServiceImpl extends ServiceImpl<DocMapper, Doc> implements IDocService {
    @Override
    public PageVO<DocVO> docList(DocDao dao) {
        Long ebookId = dao.getEbookId();
        String name = dao.getName();
        PageHelper.startPage(dao.getPage(),dao.getSize());
        List<Doc> list = this.lambdaQuery()
                .eq(ebookId != null, Doc::getEbookId, ebookId)
                .or()
                .like(name != null, Doc::getName, name)
                .list();
        List<DocVO> docVOS = CopyUtil.copyList(list, DocVO.class);
        PageInfo<DocVO> info = new PageInfo<>(docVOS);
        PageVO<DocVO> vo = new PageVO<>();
        vo.setList(docVOS);
        vo.setTotal(info.getTotal());

        return vo;
    }

    @Override
    public void docSave(DocSave save) {
        Doc copy = CopyUtil.copy(save, Doc.class);
        if (save.getId() == null){
            this.save(copy);
        }

        else {
            this.lambdaUpdate()
                    .eq(Doc::getId,copy.getId())
                    .update(copy);
        }
    }

    @Override
    public void delete(String id) {
        this.removeById(id);
    }
}
